export default function useSetInterval() {
  let timer: NodeJS.Timer | number
  const start = (fn: Function, delay?: number, immediately = false, ...args: any[]) => {
    clear()
    if (immediately) fn()
    timer = setInterval(fn, delay, ...args)
  }
  const clear = () => {
    clearInterval(timer as number)
  }
  return {
    start,
    clear
  }
}

// 轮询
export const usePolling = (fn: Function, immediately = false) => {
  let timer: NodeJS.Timer | number
  const start = (delay?: number, ...args: any[]) => {
    clear()
    if (immediately) fn()
    timer = setInterval(fn, delay, ...args)
  }
  const clear = () => {
    clearInterval(timer as number)
  }

  return {
    start,
    clear
  }
}
